﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Xml;
using Twi.COMMON.Core;

namespace Twi.DataAccess.Common
{
    public class SQLMapHelper
    {
        public static SQLMapCommandInfo GetByCode(string sqlMapFileFullPath, string code, DbProviderType dbProviderType = DbProviderType.Oracle)
        {
            SQLMapCommandInfo commandInfo = new SQLMapCommandInfo();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(sqlMapFileFullPath);
            XmlElement root = xmlDoc.DocumentElement;
            XmlNode node = default(XmlNode);
            node = root.SelectSingleNode("//SQL[@Code='" + code + "']");
            if (node == null)
            {
                throw new Exception("SQLMap中未找到Code为：【" + code + "】的配置");
            }
            if (node.ChildNodes.Count == 1) //通用SQL
            {
                commandInfo.ConfigSQL = node.InnerText;
            }
            else //非通用SQL
            {
                commandInfo.ConfigSQL = node.SelectSingleNode(TwiEnumHelper.GetName(dbProviderType == DbProviderType.OracleDataAccess ? DbProviderType.Oracle : dbProviderType)).InnerText;
            }
            commandInfo.TransferedSQL = commandInfo.ConfigSQL;
            string parameterPrefix = (dbProviderType == DbProviderType.Oracle || dbProviderType == DbProviderType.OracleDataAccess) ? ":" : "@";
            //ArrStr 中奇数将表示参数
            string[] arrStr = commandInfo.ConfigSQL.Split('#');
            for (int i = 1; i < arrStr.Length; i = i + 2)
            {
                //参数默认是按顺序的，所以不能排除，名称虽然一样，但是是不同的参数对象
                //if (!commandInfo.ParaNames.Contains(arrStr[i]))
                //{
                    commandInfo.ParaNames.Add(arrStr[i]);
                //}
                commandInfo.TransferedSQL = commandInfo.TransferedSQL.Replace("#" + arrStr[i] + "#", parameterPrefix + arrStr[i]);
            }
            return commandInfo;
        }
    }

    /// <summary>
    /// SQL-MAP 配置文件命令信息
    /// </summary>
    public class SQLMapCommandInfo
    {
        public SQLMapCommandInfo()
        {
            ParaNames = new List<string>();
        }

        /// <summary>
        /// TwiSQLMap中原始的SQL(即参数的形式为：#参数#)
        /// </summary>
        public string ConfigSQL
        {
            get;
            set;
        }

        /// <summary>
        /// 转换后的参数化SQL，如Oracle参数转换后的形式为":参数"，SQL Server转化后的参数为“@参数”
        /// </summary>
        public string TransferedSQL
        {
            get;
            set;
        }

        /// <summary>
        /// 参数名称
        /// </summary>
        public List<string> ParaNames
        {
            get;
            set;
        }

        /// <summary>
        /// 获取参数列表
        /// </summary>
        /// <param name="dictParameters">参数字典</param>
        /// <returns></returns>
        public List<DbParameter> GetDbParameters(Dictionary<string, object> dictParameters, DbUtility DB)
        {
            if (ParaNames.Count == 0)
            {
                return null;
            }
            List<DbParameter> listParams = new List<DbParameter>();
            for (int i = 0; i < ParaNames.Count; i++)
            {
                listParams.Add(DB.CreateDbParameter(ParaNames[i], dictParameters[ParaNames[i]]));
            }
            return listParams;
        }
    }


}
